package GUILayer;

import CtrlLayer.*;
//import ModelLayer.Item;
import ModelLayer.SaleItem;

import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class PopupRedigerVare extends JFrame 
{
	private ItemCtrl IC;
	private SaleItem mysaleitem;
	private JTextField textItemnr;
	private JTextField textDescription;
	private JTextField textPrice;
	private JTextField textInstock;
	private JTextField textLocation;
	private JTextField textBarcode;
	private JTextField textMinamount;
	private JTextField textMaxamount;

	public PopupRedigerVare() throws HeadlessException {
		
		IC = new ItemCtrl();
		
		// TODO Auto-generated constructor stub
		setTitle("Rediger Vare");
		JLabel emptyLabel = new JLabel("");
		emptyLabel.setPreferredSize(new Dimension(0, 230));
        
        JLabel lblVareNr = new JLabel("Varenummer");
        JLabel lblDescription = new JLabel("Beskrivelse");
        JLabel lblPrice = new JLabel("Pris");
        JLabel lblInStock = new JLabel("Antal");
        JLabel lblLocation = new JLabel("Lokation");
        JLabel lblBarCode = new JLabel("Stregkode");
        JLabel lblMinAmount = new JLabel("Minimum antal vare");
        JLabel lblMaxAmount = new JLabel("Maksimum antal vare");
        
        textItemnr = new JTextField();
        textItemnr.setColumns(10);
        
        textDescription = new JTextField();
        textDescription.setColumns(10);
        
        textPrice = new JTextField();
        textPrice.setColumns(10);
        
        textInstock = new JTextField();
        textInstock.setColumns(10);
        
        textLocation = new JTextField();
        textLocation.setColumns(10);
        
        textBarcode = new JTextField();
        textBarcode.setColumns(10);
        
        textMinamount = new JTextField();
        textMinamount.setColumns(10);
        
        textMaxamount = new JTextField();
        textMaxamount.setColumns(10);
        
        JButton btnOpretVare = new JButton("Rediger Vare");
        btnOpretVare.addActionListener(new ActionListener() 
        {
        	@Override
			public void actionPerformed(ActionEvent e) 
        	{
        		opretSaleItem();
        		dispose();
        	}
        });
        
        JButton btnAnnuller = new JButton("Annuller");
        btnAnnuller.addActionListener(new ActionListener() 
        {
        	@Override
			public void actionPerformed(ActionEvent e) 
        	{
        		dispose();
        	}
        });
        
        JButton btnSgVare = new JButton("S\u00F8g Vare");
        btnSgVare.addActionListener(new ActionListener() 
        {
        	@Override
			public void actionPerformed(ActionEvent e) 
        	{
        		getSaleItem();
        	}
        });
        
        
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(lblVareNr)
        				.addComponent(lblDescription)
        				.addComponent(lblPrice)
        				.addComponent(lblInStock)
        				.addComponent(lblLocation)
        				.addComponent(lblBarCode)
        				.addComponent(lblMinAmount)
        				.addComponent(lblMaxAmount))
        			.addGap(6)
        			.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
        				.addComponent(textItemnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textInstock, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textLocation, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textBarcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textMinamount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(textMaxamount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(81)
        			.addComponent(emptyLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(btnSgVare, GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
        				.addComponent(btnAnnuller, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
        				.addComponent(btnOpretVare, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        			.addGap(25))
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(emptyLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addContainerGap()
        					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
        						.addGroup(groupLayout.createSequentialGroup()
        							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        								.addComponent(lblVareNr)
        								.addComponent(textItemnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        								.addComponent(lblDescription)
        								.addComponent(textDescription, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        								.addComponent(lblPrice)
        								.addComponent(textPrice, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        								.addComponent(lblInStock)
        								.addComponent(textInstock, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        								.addComponent(lblLocation)
        								.addComponent(textLocation, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        							.addPreferredGap(ComponentPlacement.RELATED)
        							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        								.addComponent(lblBarCode)
        								.addComponent(textBarcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
        						.addComponent(btnSgVare))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        						.addComponent(lblMinAmount)
        						.addComponent(textMinamount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        						.addComponent(btnOpretVare))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        						.addComponent(btnAnnuller)
        						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        							.addComponent(lblMaxAmount)
        							.addComponent(textMaxamount, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))))
        			.addContainerGap())
        );
        getContentPane().setLayout(groupLayout);
        pack();
//        setVisible(true);
		
	}

	public PopupRedigerVare(GraphicsConfiguration gc) {
		super(gc);
		// TODO Auto-generated constructor stub
	}

	public PopupRedigerVare(String title) throws HeadlessException {
		super(title);
		// TODO Auto-generated constructor stub
	}

	public PopupRedigerVare(String title, GraphicsConfiguration gc) {
		super(title, gc);
		// TODO Auto-generated constructor stub
	}
	
	private void opretSaleItem()
    {
        int itemnr = Integer.parseInt(textItemnr.getText());
        String description = textDescription.getText();
        Double price = Double.parseDouble(textPrice.getText());
        int inStock = Integer.parseInt(textInstock.getText());
        String location = textLocation.getText();
        String barCode = textBarcode.getText();
        int minAmount = Integer.parseInt(textMinamount.getText());
        int maxAmount = Integer.parseInt(textMaxamount.getText());
        IC.createSaleItem(itemnr, description, price, inStock, location, barCode, minAmount, maxAmount);
    }
	
	public void getSaleItem()
	{
		int itemnr = Integer.parseInt( textItemnr.getText());
		mysaleitem = IC.searchSaleItem(itemnr);
		textItemnr.setText( Integer.toString(mysaleitem.getItemnr()));
		textDescription.setText(mysaleitem.getDescription());
		textPrice.setText(Double.toString(mysaleitem.getPrice()));
		textInstock.setText(Integer.toString(mysaleitem.getInStock()));
		textLocation.setText(mysaleitem.getLocation());
		textBarcode.setText(mysaleitem.getBarCode());
        textMinamount.setText(Integer.toString(mysaleitem.getMinAmount()));
        textMaxamount.setText(Integer.toString(mysaleitem.getMaxAmount()));
	}
	
}
